<?php
class News_category extends Admin_Controller{
	
	public $namespace = 'news';
	public $section = 'categories';
	function __construct(){
		parent::__construct();
		$this->load->model("news_category_model",'categories_m');
	}
	function index(){	
		$news_category =  $this->categories_m->get_all_parent();
		$data["model"] = $news_category;
		$this->view("admin/news_category",$data);
	}
	function bulk_save($page=0){
		$post	= $this->input->post();
		$list_update = "";
		$this->load->helper("handle_string");
		foreach($post["title"] as $id => $value){
			$list_update[] = array("title"=>$value,
								   "slug"=>utf8_to_ascii($value),
								   "status"=>$post["status"][$id],
								   "ordering"=>$post["ordering"][$id],
								   "id"=>$id,
							);
		}
		if(!empty($list_update)){
			$this->db->update_batch("news_categories",$list_update,"id");	
		}
		$this->session->set_flashdata('message',"Cập nhật thành công");
		if($page != 0)
			redirect("admin/news_category/index/".$page);
		else
			redirect("admin/news_category");
	}
	function copy_data($id,$page_index){
		$info_data = $this->db->select("title,slug,status,ordering")->where("id",$id)->get("news_categories")->row();
		
		if(!empty($info_data)){
			$rand = rand(1,100);
			$info_data->slug  = $info_data->slug."_".$rand;
			$info_data->title  = $info_data->title."(".$rand.")";
			$info_data->created = time();
			$this->db->insert("news_categories",$info_data);
			$id_insert = $this->db->insert_id();
			redirect("admin/news_category/edit/".$id_insert);
		}
		$this->session->set_flashdata('message',"Xảy ra lỗi trong quá trình copy");
		redirect("admin/news_category");
	}
	public function delete($id,$page_index){
		$this->categories_m->delete($id);
		if(!empty($page_index))
			redirect("admin/news_category/index/".$page_index);
		else
			redirect("admin/news_category");
	}
	public function edit($id=0,$method=0){
		if ($id > 0){
			$model = (array)$this->categories_m->get($id);
		} else{
			$model['id'] = $id;
		}

		if($this->input->post('slug') != @$model['slug']) {
			$is_unique =  '|is_unique[news_categories.slug]';
			$this->form_validation->set_rules('slug',"liên kết tĩnh", 'required|trim|alpha_dash'.$is_unique);
        }
		$this->form_validation->set_rules('title',"tiêu đề",'required|max_length[80]');
		if ($this->form_validation->run() == TRUE){	
			$model = $this->input->post();		
			//UPLOAD IMAGE - news_category chi up va resize ko can save hinh f			
			if ($id > 0){
				
				$this->categories_m->update($id,$model);
				$this->session->set_flashdata('message',"Cập nhật thành công");
				redirect(config_item('admin_folder').'/news_category/edit/'.$id);
			}
			else{
				$id_insert = $this->categories_m->insert($model);
				$this->session->set_flashdata('message',"Thêm mới thành công");
				redirect(config_item('admin_folder').'/news_category');
			}
		}
		$data["model"] = $model;
	
		$this->view(config_item('admin_folder').'/news_category_form',$data);
	}
}